﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Media3D;

namespace Csharp基础作业
{
    //定义接口
    public interface IBankAccount
    {
        void PayIn(decimal amount);
        bool Withdraw(decimal amount);
        decimal Balance
        {
            get;
        }
    }
    public interface IBankAccount1
    {
        void PayIn1(decimal amount);
    }
    //定义从接口继承的类
    public class SaverAccount : IBankAccount,IBankAccount1
    {
        private decimal balance;
        //实现IBankAccount接口定义的方法与属性
        public void PayIn(decimal amount)
        {
            balance += amount;
            MessageBox.Show($"Now PayIn {amount}\nbalance={balance}");
        }
        public bool Withdraw(decimal amount)
        {
            if(balance >= amount)
            {
                balance -= amount;
                MessageBox.Show($"Now Withdraw {amount}\nbalance={balance}");
                return true;
            }
            MessageBox.Show($"Try to withdraw {amount}\n" +
                $"balance={balance}<amount={amount}\n" +
                $"Withdrawal attempt failed.");
            return false;
        }
        public decimal Balance
        {
            get { return balance; }
        }
        //实现IBankAccount1接口定义的方法与属性
        public void PayIn1(decimal amount)
        {
            balance += amount;
        }
        //由类实现的不属于任何接口的方法
        public float Class_Method()
        {
            float a = 10.0f;
            return a;
        }
        //重载
        public override string ToString()
        {
            return String.Format("Venus Bank Saver: Balance = {0, 6:C}", balance);
        }
    }
    //用来运行的静态类
    public static class Work2
    {
        static public void run()
        {

            //声明IBankAccount接口引用，创建对象，调用接口定义的方法与属性
            IBankAccount venusAccount = new SaverAccount();
            venusAccount.PayIn(500);
            venusAccount.Withdraw(600);
            venusAccount.Withdraw(100);
            MessageBox.Show(venusAccount.ToString());

            //调用类定义的特有方法，实现接口到类达到转换，类似于基类到派生类的转换
            SaverAccount test = (SaverAccount)venusAccount;
            MessageBox.Show($"test.Class_Method().ToString()={test.Class_Method().ToString()}");

            //调用另外一个接口定义的方法，前两种是类到接口的转换，类似于
            //派生类到积累的转换，后一种是接口到接口的转换
            IBankAccount1 test1 = test;
            //IBankAccount1 test1 = (IBankAccount1)test;
            //IBankAccount1 test1 = (IBankAccount1)venusAccount;
            test.PayIn1(200);

            //输出
            MessageBox.Show(venusAccount.ToString());
        }
    }

}
